package com.sg.business.management.handler;

import java.util.Map;

import org.eclipse.core.commands.Command;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPart;

import com.mobnut.db.model.PrimaryObject;
import com.sg.business.model.FolderDefinition;
import com.sg.business.resource.nls.Messages;
import com.sg.widgets.MessageUtil;
import com.sg.widgets.Widgets;
import com.sg.widgets.command.AbstractNavigatorHandler;
import com.sg.widgets.part.INavigatorActionListener;
import com.sg.widgets.part.editor.DataObjectDialog;
import com.sg.widgets.registry.config.Configurator;
import com.sg.widgets.registry.config.DataEditorConfigurator;
import com.sg.widgets.viewer.ViewerControl;

public class CreateFolderDefinition extends AbstractNavigatorHandler {

	private static final String TITLE = Messages.get().CreateFolderDefinition_0;

	@Override
	protected void execute(PrimaryObject selected, IWorkbenchPart part,
			ViewerControl vc, Command command, Map<String, Object> parameters,
			IStructuredSelection selection) {
		Shell shell = part.getSite().getShell();
		if (selected instanceof FolderDefinition) {
			Configurator conf = Widgets.getEditorRegistry().getConfigurator(
					FolderDefinition.EDITOR_FOLDER_DEFINITION_CREATE);
			try {
				FolderDefinition folderd = ((FolderDefinition) selected)
						.makeFolderDefinition(null);
				DataObjectDialog.openDialog(folderd,
						(DataEditorConfigurator) conf, true, null, TITLE);
				if (part instanceof INavigatorActionListener) {

					sendNavigatorActionEvent(part,
							INavigatorActionListener.CREATE, new Integer(
									INavigatorActionListener.REFRESH));
				}
			} catch (Exception e) {
				MessageUtil.showToast(shell, TITLE, e.getMessage(),
						SWT.ICON_ERROR);
			}
		}
	}
}
